package org.lhh.server.core.codec.slave;

import lombok.Data;
import org.lhh.server.common.utils.CollectionUtils;
import org.pentaho.di.cluster.SlaveServer;
import org.pentaho.di.core.encryption.Encr;
import org.pentaho.di.repository.LongObjectId;

import java.util.ArrayList;
import java.util.List;

@Data
public class SlaveServerCodec {

    private Long id;
    private String name;
    private String hostname;
    private String port;
    private String webAppName;
    private String username;
    private String password;
    private String proxy_hostname;
    private String proxy_port;
    private String non_proxy_hosts;
    private Integer master;

    public static SlaveServerCodec encodeSingle(SlaveServer slaveServer) {
        SlaveServerCodec slaveServerCodec = new SlaveServerCodec();
        slaveServerCodec.setId(Long.valueOf(slaveServer.getObjectId().getId()));
        slaveServerCodec.setName(slaveServer.getName());
        slaveServerCodec.setHostname(slaveServer.getHostname());
        slaveServerCodec.setPort(slaveServer.getPort());
        slaveServerCodec.setWebAppName(slaveServer.getWebAppName());
        slaveServerCodec.setUsername(slaveServer.getUsername());
        slaveServerCodec.setPassword(Encr.encryptPasswordIfNotUsingVariables(slaveServer.getPassword()));
        slaveServerCodec.setProxy_hostname(slaveServer.getProxyHostname());
        slaveServerCodec.setProxy_port(slaveServer.getProxyPort());
        slaveServerCodec.setNon_proxy_hosts(slaveServer.getNonProxyHosts());
        slaveServerCodec.setMaster(slaveServer.isMaster() ? 1 : 0);
        return slaveServerCodec;
    }


    public static SlaveServer decodeSingle(SlaveServerCodec slaveServerCodec) {
        SlaveServer slaveServer = new SlaveServer();
        if (slaveServerCodec.getId() != null)
            slaveServer.setObjectId(new LongObjectId(slaveServerCodec.getId()));
        slaveServer.setName(slaveServerCodec.getName());
        slaveServer.setHostname(slaveServerCodec.getHostname());
        slaveServer.setPort(slaveServerCodec.getPort());
        slaveServer.setWebAppName(slaveServerCodec.getWebAppName());
        slaveServer.setUsername(slaveServerCodec.getUsername());
        slaveServer.setPassword(Encr.decryptPasswordOptionallyEncrypted(slaveServerCodec.getPassword()));
        slaveServer.setProxyHostname(slaveServerCodec.getProxy_hostname());
        slaveServer.setProxyPort(slaveServerCodec.getProxy_port());
        slaveServer.setNonProxyHosts(slaveServerCodec.getNon_proxy_hosts());
        slaveServer.setMaster(slaveServerCodec.getMaster() == 1);
        return slaveServer;
    }

    public static List<SlaveServerCodec> encode(List<SlaveServer> slaveServers) {
        List<SlaveServerCodec> slaveServerCodecs = new ArrayList<>();
        if (!CollectionUtils.isEmpty(slaveServers)) {
            for (SlaveServer slaveServer : slaveServers) {
                slaveServerCodecs.add(encodeSingle(slaveServer));
            }
        }
        return slaveServerCodecs;
    }

    public static List<SlaveServer> decode(List<SlaveServerCodec> slaveServerCodecs) {
        List<SlaveServer> slaveServers = new ArrayList<>();
        if (!CollectionUtils.isEmpty(slaveServerCodecs)) {
            for (SlaveServerCodec slaveServerCodec : slaveServerCodecs) {
                slaveServers.add(decodeSingle(slaveServerCodec));
            }
        }
        return slaveServers;
    }
}
